package com.sinosoft.msf.base.supplier.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSONObject;
import com.sinosoft.msf.base.supplier.expection.BizException;
import com.sinosoft.msf.base.supplier.pojo.*;
import com.sinosoft.msf.base.supplier.service.CommonBizService;
import com.sinosoft.msf.base.supplier.service.CompanyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/company")
public class CompanyController {
    private static final  Logger log = LoggerFactory.getLogger(CompanyController.class);
    @Autowired
    CompanyService companyService;
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    CommonBizService bizService;
    private OptVo curentOpt() {
        return bizService.queryCureUserInfo();
    }
    @PostMapping(value = "/save", name = "添加供应商")
    public RestEntity addCompany(HttpServletRequest request, @RequestBody CompanyInfoPojo companyInfoPojo) {
        Validator.validateNotEmpty(companyInfoPojo.getId(), "供应商id不能为空");
        Validator.validateTrue(CollUtil.isNotEmpty(companyInfoPojo.getSupplierScope()), "供应商承接范围分类不能为空");
        List<CompanyInfoPojo.ScopeItem> collect = companyInfoPojo.getSupplierScope().stream()
                .filter(item -> CollUtil.isNotEmpty(item.getCode())).collect(Collectors.toList());
        if (CollUtil.isEmpty(collect)) {
            throw new BizException("供应商承接范围分类不能为空");
        }
        List<String> codeList = companyInfoPojo.getSupplierScope().stream()
                .filter(item -> CollUtil.isNotEmpty(item.getCode()))
                .map(item -> String.join(",", item.getCode()))
                .filter(item -> !item.contains("99")).collect(Collectors.toList());
        Set<String> codeSet = new HashSet<>(codeList);
        if (codeList.size() != codeSet.size()) {
            throw new BizException("供应商承接范围分类选择重复，请重新选择");
        }
        String clientIP = ServletUtil.getClientIP(request);
        String msg = companyService.saveCompany(clientIP, companyInfoPojo);
        return RestEntity.success(msg);
    }

    @PostMapping(value = "/query", name = "供应商列表查询")
    public RestEntity queryCompany(@RequestBody CompanyQueryReqPojo companyQueryReqPojo) {
        OptVo optVo = curentOpt();
        return RestEntity.data(companyService.queryCompany(companyQueryReqPojo,optVo));
    }

    @PostMapping(value = "/detail", name = "供应商详情查询")
    public RestEntity queryCompanyDetail(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        String projectId = jsonObject.getString("projectId");
        Validator.validateNotEmpty(id, "供应商id不可为空");
        return RestEntity.data(companyService.queryCompanyDetail(id,projectId));
    }

    @PostMapping(value = "/audit/legal/submit", name = "提交法务审核结果")
    public RestEntity submitLegal(@RequestBody JSONObject jsonObject) {
        Validator.validateNotEmpty(jsonObject.getString("id"), "供应商id不可为空");
        Validator.validateNotEmpty(jsonObject.getString("auditResult"), "审核结论不可为空");
//        Validator.validateNotEmpty(jsonObject.getString("auditRemark"), "审核意见不可为空");
        OptVo optVo = curentOpt();
        companyService.submitLegal(jsonObject,optVo);
        return RestEntity.success();
    }

    @PostMapping(value = "/audit/admin/submit", name = "提交管理员审核结果")
    public RestEntity submitAdmin(@RequestBody JSONObject jsonObject) {
        Validator.validateNotEmpty(jsonObject.getString("id"), "供应商id不可为空");
        Validator.validateNotEmpty(jsonObject.getString("auditResult"), "审核结论不可为空");
//        Validator.validateNotEmpty(jsonObject.getString("auditRemark"), "审核意见不可为空");
        OptVo optVo = curentOpt();
        companyService.submitAdmin(jsonObject,optVo);
        return RestEntity.success();
    }

    @PostMapping(value = "/withdraw", name = "供应商信息撤回")
    public RestEntity withdrawCompany(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        Validator.validateNotEmpty(id, "供应商id不可为空");
        companyService.withdrawCompany(id);
        return RestEntity.success();
    }

    @PostMapping(value = "/list", name = "供应商下拉列表查询")
    public RestEntity queryList(@RequestBody JSONObject obj) {
        return RestEntity.data(companyService.queryList(obj));
    }

    @PostMapping(value = "/report/query", name = "供应商报表查询")
    public RestEntity queryCompanyReport(@RequestBody ReportQueryReqPojo reportQueryReqPojo) {
        return RestEntity.data(companyService.queryCompanyReport(reportQueryReqPojo));
    }

    @PostMapping(value = "/report/export", name = "供应商报表导出")
    public void exportCompanyReport(@RequestBody ReportParamData paramData, HttpServletResponse response) {
        companyService.exportCompanyReport(paramData, response);
    }
    @PostMapping(value = "/mod/hisList", name = "修改历史")
    public RestEntity modHisList(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        Validator.validateNotEmpty(id, "供应商id不可为空");
        return RestEntity.data(companyService.modHisList(id));
    }
    @PostMapping(value = "/mod/hisDetail", name = "修改历史")
    public RestEntity modHisDetail(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        Validator.validateNotEmpty(id, "供应商id不可为空");
        return RestEntity.data(companyService.modHisDetail(id));
    }

    @PostMapping(value = "/mark/submit", name = "标记管理提交审批")
    public RestEntity markSubmit(@RequestBody CompanyMarkReq markPojo) {
        Validator.validateNotEmpty(markPojo.getCompanyId(), "供应商id不可为空");
        Validator.validateNotEmpty(markPojo.getMarkStatus(), "标记状态不可为空");
        OptVo optVo = curentOpt();
        companyService.markSubmit(markPojo, optVo);
        return RestEntity.success();
    }

    @PostMapping(value = "/mark/hisList", name = "标记管理历史记录")
    public RestEntity markHisList(@RequestBody JSONObject jsonObject) {
        String companyId = jsonObject.getString("companyId");
        Validator.validateNotEmpty(companyId, "供应商id不可为空");
        return RestEntity.data(companyService.markHisList(jsonObject));
    }

    @PostMapping(value = "/mark/audit/submit", name = "标记管理提交管理员审核结果")
    public RestEntity markAuditSubmit(@RequestBody CompanyMarkAuditReq markAuditReq) {
        Validator.validateNotEmpty(markAuditReq.getCompanyId(), "供应商id不可为空");
        Validator.validateNotEmpty(markAuditReq.getMarkId(), "标记id不可为空");
        Validator.validateNotEmpty(markAuditReq.getAuditResult(), "审核结论不可为空");
        OptVo optVo = curentOpt();
        companyService.markAuditSubmit(markAuditReq, optVo);
        return RestEntity.success();
    }

    @PostMapping(value = "/mark/query", name = "标记管理待办查询")
    public RestEntity markQuery(@RequestBody CompanyQueryReqPojo companyQueryReqPojo) {
        OptVo optVo = curentOpt();
        return RestEntity.data(companyService.markQuery(companyQueryReqPojo, optVo));
    }

    @PostMapping(value = "/notice/sendEmail", name = "发送修改链接邮件")
    public RestEntity noticeSendEmail(@RequestBody JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        Validator.validateNotEmpty(id, "供应商id不可为空");
        companyService.noticeSendEmail(jsonObject);
        return RestEntity.success();
    }
}
